using System;
using System.Collections.Generic;
using System.Text;

namespace LoggingClient
{
    public abstract class AbstractLogger : ILogger
    {
        private static int indent = 0;
        private static readonly int INDENT_TAB_SIZE = 2;

        protected abstract void WriteLine(LogLevel level, string log);


        public void Error(object obj)
        {
            WriteLine(LogLevel.Error, FormatIndentation(obj.ToString()));
        }

        public void Info(object obj)
        {
            WriteLine(LogLevel.Info, FormatIndentation(obj.ToString()));
        }

        public void Debug(object obj)
        {
            WriteLine(LogLevel.Debug, FormatIndentation(obj.ToString()));
        }

        public string FormatLine(LogLevel level, string log)
        {
            string logStr = string.Empty;

            switch (level)
            {
                case LogLevel.Debug:
                    logStr = string.Format("{0:d/M/yyyy HH:mm:ss} {1,-8} {2,-20} {3,-400}", DateTime.Now, "Debug", mModule, log);
                    break;
                case LogLevel.Error:
                    logStr = string.Format("{0:d/M/yyyy HH:mm:ss} {1,-8} {2,-20} {3,-400}", DateTime.Now, "Error", mModule, log);
                    break;
                default:
                    logStr = string.Format("{0:d/M/yyyy HH:mm:ss} {1,-8} {2,-20} {3,-400}", DateTime.Now, "Info", mModule, log);
                    break;
            }

            if (log.Length > 400)
            {
                logStr += "/r/n" + "Previous log was truncated";
            }

            return logStr;
        }

        protected string mModule = "Unknown";
        public void Init(string module)
        {
            mModule = module.Length > 16 ? module.Substring(0, 16) : module;
        }



        public void Indent()
        {
            indent++;
        }

        public void Unindent()
        {
            indent--;
        }

        public string FormatIndentation(string msg)
        {
            msg = msg.TrimEnd();

            if (indent == 0)
                return msg;
            return new string(' ', indent * INDENT_TAB_SIZE) + msg;
        }

    }
}
